package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.Material;
import com.banksoft.cps.bean.query.MaterialQuery;
import com.banksoft.cps.dao.MaterialDao;
import com.banksoft.cps.service.MaterialService;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/12.
 */
@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    MaterialDao materialDao;
    @Override
    public List<Material> list(MaterialQuery query) {
        return materialDao.list(query);
    }

    @Override
    public long count(MaterialQuery query) {
        return materialDao.count(query);
    }

    @Override
    public void save(Material bean) {
        if(StringUtils.isNotBlank(bean.getId())){
            materialDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());
            materialDao.insert(bean);
        }
    }

    @Override
    public Material findById(String id) {
        if(StringUtils.isNotBlank(id)){
            MaterialQuery query = new MaterialQuery();
            query.setId(id);
            List<Material>  list = materialDao.list(query);
            if(!list.isEmpty()){
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    public void delete(String[] ids) {
        materialDao.delete(ids);
    }
}
